package com.firefly.web.controller.home.view;

import com.firefly.dao.dao.cms.po.ArticlePo;
import com.firefly.service.query.cms.ArticleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @description:
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2023/2/2
 **/
@Controller
public class ArticleViewController {
    private final ArticleQuery articleQuery;
    @Autowired
    public ArticleViewController(ArticleQuery articleQuery){
        this.articleQuery = articleQuery;
    }
    @GetMapping("/cate/{url}")
    public ModelAndView getList(@PathVariable(value = "url") String url){
        ModelAndView modelAndView = new ModelAndView();
        try {
            Map result = articleQuery.getListForUrl(url);
            modelAndView.addObject("list", result.get("list"));
            modelAndView.addObject("cate", result.get("cate"));
            modelAndView.setViewName("category");
        }catch (Exception e){
            modelAndView.setViewName("404");
        }
        return modelAndView;
    }

    @GetMapping("/article/{url}")
    public ModelAndView getArticle(@PathVariable(value = "url") String url){
        ModelAndView modelAndView = new ModelAndView();
        try {
            ArticlePo result = articleQuery.getDetail(url);
            modelAndView.addObject("data", result);
            modelAndView.setViewName("article");
        }catch (Exception e){
            modelAndView.setViewName("404");
        }
        return modelAndView;
    }
}
